/*
 * Copyright (C), 2002-2018, 你的兔宝宝电子商务有限公司
 * FileName: NetController.java
 * Author:   你的兔宝宝
 * Date:     2018-2-13 下午5:21:15
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.baby.demo.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.util.JSONPObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baby.demo.service.NetService;
import com.baby.demo.utils.Collections;
import com.baby.demo.utils.FileUtil;
import com.baby.demo.utils.Maps;
import com.baby.demo.utils.Strings;
import com.baby.demo.utils.net.CURL;

/**
 * 网络相关<br> 
 * 〈功能详细描述〉
 *
 * @author 你的兔宝宝
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@RequestMapping("/net")
public class NetController {
    
    @Autowired
    NetService netService;
    
    @RequestMapping("curl")
    @ResponseBody
    public JSONPObject curl(HttpServletRequest request, HttpServletResponse response
            ,@RequestParam(value="params") Map<String,Object> params,@RequestParam(value="callback") String callback){
        Map<String,Object> result = Maps.noop();
        result.put("success", false);
        if( Collections.isNotEmpty(params) ){
            try {
                String url = Strings.get(params, "url");
                String filepath = "E:\\js-project\\ui\\";
                CURL curl = new CURL();
                curl.proxy("10.19.110.55", "8080");
                String stream = curl.curl(url);
                String host = url.substring(0,url.indexOf("code"));
                String path = Strings.searchOne(stream, ".*?<iframe id=\"if\" src=\"([^\"]+?)\">.*", "$1");
                String title = Strings.searchOne(stream, "<title>([^<]+)</title>", "$1");
                System.out.println(host+path+title);
                System.out.println(filepath+title+".html");
                String stream1 = curl.curl(host+path);
                stream1 = stream1.substring(0,stream1.lastIndexOf("\n"));
                FileUtil.writeFile(filepath+title+".html",stream1);
                result.put("success", true);
            } catch (Exception e) {
                result.put("success", false);
                result.put("error", e.getMessage());
            }
        }
        JSONPObject jsonp = new JSONPObject(callback, result);
        return jsonp;
    }
    
    @RequestMapping("curl-demo")
    @ResponseBody
    public JSONPObject curlDemo(HttpServletRequest request, HttpServletResponse response
            ,@RequestParam(value="params") Map<String,Object> params,@RequestParam(value="callback") String callback){
        Map<String,Object> result = Maps.noop();
        result.put("success", false);
        if( Collections.isNotEmpty(params) ){
            try {
                netService.downloadJqDemo(params);
                result.put("success", true);
            } catch (Exception e) {
                result.put("success", false);
                result.put("error", e.getMessage());
            }
        }
        JSONPObject jsonp = new JSONPObject(callback, result);
        return jsonp;
    }
    
    @RequestMapping("curl-demo-resource")
    @ResponseBody
    public JSONPObject curlDemoResource(HttpServletRequest request, HttpServletResponse response
            ,@RequestParam(value="params") Map<String,Object> params,@RequestParam(value="callback") String callback){
        Map<String,Object> result = Maps.noop();
        result.put("success", false);
        if( Collections.isNotEmpty(params) ){
            try {
                netService.downloadJqDemoResource(params);
                result.put("success", true);
            } catch (Exception e) {
                result.put("success", false);
                result.put("error", e.getMessage());
            }
        }
        JSONPObject jsonp = new JSONPObject(callback, result);
        return jsonp;
    }
    
    @RequestMapping("curl-page")
    @ResponseBody
    public JSONPObject curlPage(HttpServletRequest request, HttpServletResponse response
            ,@RequestParam(value="params") Map<String,Object> params,@RequestParam(value="callback") String callback){
        Map<String,Object> result = Maps.noop();
        result.put("success", false);
        if( Collections.isNotEmpty(params) ){
            try {
                netService.downloadPage(params);
                result.put("success", true);
            } catch (Exception e) {
                result.put("success", false);
                result.put("error", e.getMessage());
            }
        }
        JSONPObject jsonp = new JSONPObject(callback, result);
        return jsonp;
    }
    
    @RequestMapping("curl-page-resource")
    @ResponseBody
    public JSONPObject curlPageResource(HttpServletRequest request, HttpServletResponse response
            ,@RequestParam(value="params") Map<String,Object> params,@RequestParam(value="callback") String callback){
        Map<String,Object> result = Maps.noop();
        result.put("success", false);
        if( Collections.isNotEmpty(params) ){
            try {
                netService.downloadPageResource(params);
                result.put("success", true);
            } catch (Exception e) {
                result.put("success", false);
                result.put("error", e.getMessage());
            }
        }
        JSONPObject jsonp = new JSONPObject(callback, result);
        return jsonp;
    }
}
